<template>
  <div class="tracetask-container">
    <v-layout wrap justify-center align-center>
      <v-flex md12 d-flex>
        <v-flex md2 px-2>
          <v-menu offset-x full-width>
            <v-text-field
              color="primary"
              slot="activator"
              v-model="queryForm.beginTime"
              placeholder="开始时间"
              disabled
            ></v-text-field>
            <v-date-picker
              landscape
              min="2019-07-01"
              :first-day-of-week="0"
              locale="zh-cn"
              v-model="queryForm.beginTime"
              color="secondary"
            ></v-date-picker>
          </v-menu>
        </v-flex>
        <v-flex md2 px-2>
          <v-menu offset-x full-width>
            <v-text-field
              color="primary"
              slot="activator"
              v-model="queryForm.endTime"
              placeholder="结束时间"
              disabled
            ></v-text-field>
            <v-date-picker
              landscape
              min="2019-07-01"
              :first-day-of-week="0"
              locale="zh-cn"
              v-model="queryForm.endTime"
              color="secondary"
            ></v-date-picker>
          </v-menu>
        </v-flex>

        <v-flex md2 px-2>
          <v-btn round class="subTitle" color="primary" @click="onQuery">
            <span>查询</span>
            <v-icon>search</v-icon>
          </v-btn>
        </v-flex>
      </v-flex>

      <!-- table -->
      <v-flex md12 class="elevation-1">
        <v-data-table :headers="headers" :loading="loading" :items="tableData" hide-actions>
          <template v-slot:items="props">
            <td class="text-xs-center">{{ getTime(props.item.dataTime) }}</td>
            <td class="text-xs-center">{{ props.item.taskNum }}</td>
            <td class="text-xs-center">{{ props.item.orderReceivingNum }}</td>
            <td class="text-xs-center">{{ props.item.orderCompletionNum }}</td>
            <td class="text-xs-center">{{ props.item.rechargeAmount }}</td>
            <td class="text-xs-center">{{ props.item.withdrawAmount }}</td>
            <td class="text-xs-center">{{ props.item.businessRegistration }}</td>
            <td class="text-xs-center">{{ props.item.buyerRegistration }}</td>
          </template>
          <template v-slot:no-data>
            <v-alert :value="true" outline color="warning" icon="info">暂无数据</v-alert>
          </template>
        </v-data-table>

        <v-layout class="pagination-wrapper">
          <v-flex md10 d-flex justify-start align-center>
            <v-flex d-flex md2 justify-start align-center>
              <v-flex md4 class="title-color">显示</v-flex>
              <v-flex md4>
                <v-select
                  @change="onPageOptionChange"
                  :items="pageOptions"
                  v-model="pagination.limit"
                ></v-select>
              </v-flex>
              <v-flex md4 class="title-color">条/页</v-flex>
            </v-flex>
            <v-flex md2 class="title-color">共 {{ pagination.pageCount }} 条数据</v-flex>
            <v-flex md2></v-flex>
            <v-flex md4>
              <v-pagination
                @input="onPageChange"
                :length="pagination.pageCount"
                circle
                total-visible="5"
                v-model="pagination.page"
              />
            </v-flex>
            <v-flex md2></v-flex>
          </v-flex>
        </v-layout>
      </v-flex>
    </v-layout>
  </div>
</template>


<script lang="ts">
// 使用类装饰器
import { Component, Vue } from "vue-property-decorator";
import { System } from "@/api";
import { forEach } from "lodash";
import { KV } from "@/utils/type";
import { PageOptions } from "@/utils/constants";
import { getTime } from "@/utils/commonUtils";
import { ReqDataStatistics } from '../../api/bean';
declare const $Vue: Vue;

@Component
export default class MyBills extends Vue {
  private pageOptions: any = [];

  private states: any = [
    {
      text: "",
      value: 0
    },
    {
      text: "已发布",
      value: 1
    },
    {
      text: "未发布",
      value: 2
    }
  ];

  private queryForm: any = {
    beginTime: "",
    endTime: ""
  };

  private headers: Array<any> = [
    { text: "时间", align: "center", sortable: false, value: "id" },
    { text: "发布任务数", align: "center", sortable: false, value: "taskId" },
    { text: "接单数", align: "center", sortable: false, value: "platform" },
    { text: "完成订单数", align: "center", sortable: false, value: "type" },
    { text: "充值金额", align: "center", sortable: false, value: "seller" },
    { text: "提现金额", align: "center", sortable: false, value: "myshop" },
    { text: "商家注册人次", align: "center", sortable: false, value: "censor" },
    {
      text: "买手注册人次",
      align: "center",
      sortable: false,
      value: "createTime"
    }
  ];

  private tableData: any = [
  ];

  private loading: boolean = false;
  private dialog: boolean = false;
  private dialogItem: any = {};
  private isQuery: boolean = false;

  private pagination: any = {
    pageCount: 20,
    page: 1,
    limit: 10,
    offset: 0
  };

  private getTime(date: string) {
    return getTime(date);
  }

  protected mounted() {
    this.pageOptions = PageOptions.slice();
    this.getDataStatistics();

  }

  private onQuery(from?: string) {
    let params: any = {};

    let valid = 0;
    forEach(this.queryForm, (item, key) => {
      item ? (params[key] = item) : valid++;
    });
    if (valid === 2) {
      return $Vue.$emit("snackbar", {
        message: "请至少填写一个查询条件"
      });
    }

    if (from === undefined) {
      this.pagination.offset = 0;
      this.pagination.page = 1;
    }

    if (this.queryForm.beginTime && this.queryForm.endTime) {
      if (
        new Date(this.queryForm.beginTime).getTime() >
        new Date(this.queryForm.endTime).getTime()
      ) {
        return $Vue.$emit("snackbar", {
          text: "起始日期不能早于结束日期"
        });
      }
      params.endTime = new Date(this.queryForm.endTime + " 23:59:59").getTime();
      params.beginTime = new Date(
        this.queryForm.beginTime + " 00:00:00"
      ).getTime();
    } else if (this.queryForm.beginTime || this.queryForm.endTime) {
      return $Vue.$emit("snackbar", {
        text: "请选择完整起止日期"
      });
    }
    params.limit = this.pagination.limit;
    params.offset = this.pagination.offset;
    this.isQuery = true;
    this.getDataStatistics(params);
  }

  private onPageChange(page: number) {
    let limit = this.pagination.limit;
    this.pagination.offset = (page - 1) * limit;
    if (!this.isQuery) {
      this.getDataStatistics();
    } else {
      this.onQuery("wyvonj");
    }
  }

  private onPageOptionChange(value: any) {
    this.pagination.page = 1;
    this.pagination.offset = 0;
  }

  private validate() { }

  /**
   * 获取任务列表
   */
  private async getDataStatistics(queryForm?: any) {
    let params: ReqDataStatistics = {
      offset: this.pagination.offset,
      limit: this.pagination.limit
    };
    if (queryForm) {
      params = queryForm;
    }
    const { data } = await System.getDataStatistics.r(params);
    console.log("getDataStatistics", data);
    if (data.result === 0) {
      let $data = data.data;
      this.tableData = $data.dataList;
      this.pagination.pageCount = $data.totalCount
        ? Math.ceil($data.totalCount / this.pagination.limit)
        : 1;
    } else {
      $Vue.$emit("snackbar", {
        message: data.message
      });
    }
  }

}
</script>

<style lang="less" scoped>
.tracetask-container {
  width: 100%;
  height: 100%;
}
.pagination-wrapper {
  background: white;
  border-top: 1px solid #d4d4d4;
}
</style>


